var interval = null;
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    phone: "",
    time: 60,
    code: null,
    reget: false,
    topTips: false,
    code_isFocus: true, //控制input 聚焦
    code: [],
    focus_status: [],
    length: 0, //已经输入的长度
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    this.setPhone(options.phone);
    //倒计时函数
    that.getCode();
    that.set_Focus();
  },
  /**
   * 设置手机号
   */
  setPhone: function(phoneNumber) {
    this.setData({
      phone: phoneNumber
    })
  },
  //验证码输入时获取验证码
  get_code(e) {
    var that = this;
    that.setData({
      code: e.detail.value
    });
    if (that.data.code.length == 0) {
      that.setData({
        focus_status: "1000"
      });
    }
    if (that.data.code.length == 1) {
      that.setData({
        length: e.detail.value.length,
        focus_status: "0100"
      });
    }
    if (that.data.code.length == 2) {
      that.setData({
        length: e.detail.value.length,
        focus_status: "0010"
      });
    }
    if (that.data.code.length == 3) {
      that.setData({
        length: e.detail.value.length,
        focus_status: "0001"
      });
    }
    if (that.data.code.length == 4) {
      that.setData({
        length: e.detail.value.length
      })
      that.doLogin();
    }
  },

  set_Focus() { //聚焦input
    var that = this
    that.setData({
      code_isFocus: true
    })
  },


  //倒计时函数
  getCode: function(options) {
    var that = this;
    var currentTime = that.data.time
    interval = setInterval(function() {
      currentTime--;
      that.setData({
        time: currentTime
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: "",
          currentTime: 11,
          disabled: false,
          reget: true, //改变字体样式颜色
          currentTime: 61,
          disabled: false
        })
      }
    }, 1000)
  },

  //重新获取验证码
  reGetCode: function() {
    this.setData({
      time: 60,
      disabled: true,
      reget: false, //改变字体样式颜色
      currentTime: 61,
      disabled: true
    });
    this.getCode();
    this.set_Focus();
  },

  /**
   * 发起登录
   */
  doLogin: function() {
    var me = this;
    if (this.validateCode() === false) return;
    wx.showLoading({
      title: '正在登录...',
      mask: true,
      success: function(resp) {

      }
    })
    wx.request({
      url: app.getUrl('Login'),
      method: 'POST',
      data: {
        tel: this.data.phone,
        openId: ''
      },
      header: app.getRequestHeader(),
      success: function(res) {
        wx.hideLoading();
        if (res.data.data===1) {
          //wx.setStroage(app.cacheKey.userInfo_phone,me.data.phone);
          wx.redirectTo({
            url: '/pages/room/list?phone=' + me.data.phone
          })
        }else{
          wx.showToast({
            title: res.data.message||'当前登录手机非系统用户！',
            icon: 'none',
            duration: 4000
          })
        }
      }
    })

  },
  validateCode: function() {
    // if (this.data.code == '1234' || this.data.code=='1111' || this.data.code=='2222') {
    //   wx.showToast({
    //     title: '验证码错误，请重新输入！',
    //     icon: 'none',
    //     duration: 2000
    //   })
    //   return false;
    // }
    return true;
  }
})